package com.ydgk.service.impl;

import com.ydgk.bean.*;
import com.ydgk.dao.*;
import com.ydgk.exception.DeleteException;
import com.ydgk.service.UserService;
import com.ydgk.vo.AddressInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ProvinceMapper provinceMapper;

    @Autowired
    private CityMapper cityMapper;

    @Autowired
    private DistrictMapper districtMapper;

    @Autowired
    private AreaMapper areaMapper;

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private Pre_orderMapper pre_orderMapper;

    @Autowired
    private LogisticsMapper logisticsMapper;

    @Autowired
    private MessageMapper messageMapper;

    public void insert(User user) {
         userMapper.insert(user);
    }

    public Boolean checkUemailExist(String uemail) {
        int i = userMapper.findCountByUemail(uemail);
        if(i > 0){
            return true;
        }else {
            return false;
        }
    }

    public Boolean checkUnickExist(String unick) {
        int i = userMapper.findCountByUnick(unick);
        if(i > 0){
            return true;
        }else {
            return false;
        }
    }

    public Boolean checkUphoneExist(String uphone) {
        int i = userMapper.findCountByUphone(uphone);
        if(i > 0){
            return true;
        }else {
            return false;
        }
    }

    public User findByUpassAndUemail(String uemail, String upass) {
       return userMapper.findByUemailAndUpass(uemail,upass);
    }

    public User findByUpassAndUnick(String unick, String upass) {
        return userMapper.findByUnickAndUpass(unick,upass);
    }

    public User findByUpassAndUphone(String uphone, String upass) {
        return userMapper.findByUphoneAndUpass(uphone,upass);
    }

    public User findById(Integer uid) {
        return userMapper.findById(uid);
    }

    public void updateById(User user) {
       userMapper.updateById(user);
    }

    public List<Province> getAllPro() {
        return provinceMapper.findAll();
    }

    public List<City> getCityByPid(Integer pid) {
        return cityMapper.findByPid(pid);
    }

    public List<District> getDistrictByCid(Integer cid) {
        return districtMapper.findByCid(cid);
    }

    public List<Area> getAreaByDid(Integer did) {
        return areaMapper.findByDid(did);
    }

    public void insertAddress(Address address) {
        addressMapper.insert(address);
    }

    public List<Address> getAdd(int uid) {
        return addressMapper.getAdd(uid);
    }

    public AddressInfo getAddressInfo(Address address){
        Area area = areaMapper.findById(address.getAr_arid());
        District district = districtMapper.findById(area.getD_did());
        City city = cityMapper.findById(district.getC_cid());
        Province province = provinceMapper.findById(city.getP_pid());
        return new AddressInfo(address,area,district,city,province);
    }


    public void deleteAddress(Integer adid) {
        List<Order> orderList = orderMapper.findByAdid(adid);
        if(orderList.size() > 0){
            throw new DeleteException();
        }else{
            for (Order o : orderList) {
                pre_orderMapper.deleteByOid(o.getOid());
                Logistics logistics = logisticsMapper.findByOid(o.getOid());
                if(logistics != null){
                    messageMapper.deleteByLid(logistics.getLid());
                    logisticsMapper.deleteByOid(o.getOid());
                }
                orderMapper.deleteById(o.getOid());
            }
            orderMapper.deleteByAdid(adid);
            addressMapper.deleteByid(adid);
        }

    }

    public Address getAddress(Integer adid) {
        return addressMapper.findById(adid);
    }

    public void updateAddress(Address address) {
        addressMapper.updateById(address);
    }

    public int getAddCount(Integer uid) {
        return addressMapper.getAddCount(uid);
    }

    public void setAddDefault(Integer adid) {
        addressMapper.setIspreFalse();
        addressMapper.setIspreTrueById(adid);
    }
}
